function [reg_map,reg_indices]=reg_vector2reg_map(seg_map_size,hypothesis,seg_indices,bw_factor,med_filter)

% This function built a region's map from a hypothesis.

% Function Inputs:
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% seg_map_size - the size of the segments' map - (width,height).
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% hypothesis - a cell, where each member is an array of segments which belong to a certain region.
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% seg_indices - cell of all indices per segment.
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

% Function Outputs:
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% reg_map - map of each connectivity part as a different region.
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% reg_indices -  cell that contains the indices of each region.
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

[temp_reg_map,reg_map] = deal(zeros(seg_map_size));
last_value=0;
if (strcmp(med_filter,'false'))
    %Creating a map of regions
    for i=1:size(hypothesis,2)
        reg = hypothesis{i};
        for j=1:length(reg)
            indices=seg_indices{1,reg(j)};
            temp_reg_map(indices)=i;
        end
        % Creating a map of connectivity parts of each region 
        BW = (temp_reg_map == i);
        L = bwlabel(BW,bw_factor);
        indices=find(L~=0);
        reg_map(indices)=L(indices)+last_value;
        num_reg=max(max(L));
        % extracting region indices
        L_values=L(indices);
        for k=1:num_reg
            ind=find(L_values==k);
            reg_indices{1,last_value+k}=indices(ind);
        end
        last_value=last_value+num_reg;
    end
end

if (strcmp(med_filter,'true'))
    %Creating a map of regions
    for i=1:size(hypothesis,2)
        reg = hypothesis{i};
        for j=1:length(reg)
            indices=seg_indices{1,reg(j)};
            temp_reg_map(indices)=i;
        end
        % Creating a map of connectivity parts of each region 
        BW = (temp_reg_map == i);
        L = bwlabel(BW,bw_factor);
        indices=find(L~=0);
        reg_map(indices)=L(indices)+last_value;
        num_reg=max(max(L));
        % extracting region indices
        L_values=L(indices);
        for k=1:num_reg
            ind=find(L_values==k);
            reg_indices{1,last_value+k}=indices(ind);
        end
        last_value=last_value+num_reg;
    end
end

